package com.ems.dao;

import com.ems.model.Department;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class DepartmentDAO {
    public List<Department> getAllDepartments() throws SQLException {
        List<Department> departments = new ArrayList<>();
        String sql = "SELECT * FROM departments";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql);
             ResultSet rs = stmt.executeQuery()) {
            while (rs.next()) {
                departments.add(new Department(
                        rs.getInt("dept_id"),
                        rs.getString("dept_name")
                ));
            }
        }
        return departments;
    }

    public boolean addDepartment(Department dept) throws SQLException {
        String sql = "INSERT INTO departments (dept_name) VALUES (?)";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)) {
            stmt.setString(1, dept.getDeptName());
            return stmt.executeUpdate() > 0;
        }
    }

    public boolean deleteDepartment(int deptId) throws SQLException {
        String sql = "DELETE FROM departments WHERE dept_id = ?";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)) {
            stmt.setInt(1, deptId);
            return stmt.executeUpdate() > 0;
        }
    }
}